//
//  XYReadBookIndexHeaderView.swift
//  XYReadBook
//
//  Created by fox on 2017/9/29.
//  Copyright © 2017年 tsaievan. All rights reserved.

import UIKit

protocol XYReadBookIndexHeaderViewDelegate:NSObjectProtocol {
    /// 刷新
    func booklistRefresh(bookListView:XYReadBookIndexHeaderView)

    /// 倒序
    func bookChapterlistSort(bookListView:XYReadBookIndexHeaderView)

}

class XYReadBookIndexHeaderView: UIView {
    weak var delegate:XYReadBookIndexHeaderViewDelegate?
    var chapterCountLab = UILabel()
    var refreshBtn = UIButton()
    var sortBtn = UIButton()
    var chapterCount:Int
    init(frame: CGRect,chapterCount:Int) {
        self.chapterCount = chapterCount
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setupUI() {
        backgroundColor = UIColor.hm_color(withRed: 242, green: 242, blue: 242)
        addSubview(chapterCountLab)
        addSubview(refreshBtn)
        addSubview(sortBtn)
        let countString = String.init(format: "共%zd章节", chapterCount)
        chapterCountLab.text = countString
        chapterCountLab.font = UIFont.systemFont(ofSize: 14)
        refreshBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)

        refreshBtn.setTitle("刷新", for: .normal)
        refreshBtn.setImage(UIImage.init(named: "refresh"), for: .normal)
        sortBtn.setImage(UIImage.init(named: "sort"), for: .normal)
        refreshBtn.addTarget(self, action: #selector(refreshChapters), for: .touchUpInside)
        refreshBtn.setTitleColor(UIColor.orange, for: .normal)
        sortBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        sortBtn.setTitle("倒序", for: .normal)
        sortBtn.setTitle("正序", for: .selected)
        sortBtn.setTitleColor(UIColor.orange, for: .normal)
        sortBtn.setTitleColor(UIColor.orange, for: .selected)
        sortBtn.addTarget(self, action: #selector(changeSort), for: .touchUpInside)

    }

    override func layoutSubviews() {
        super.layoutSubviews()
        chapterCountLab.frame = CGRect.init(x: 12, y: 0, width: 120, height: 44)
        refreshBtn.frame = CGRect.init(x: ScreenWidth * 0.8 - 160, y: 0, width: 80, height: 44)
        sortBtn.frame = CGRect.init(x: refreshBtn.frame.maxX, y: 0, width: 80, height: 44)
    }

    func refreshChapters() {
        delegate?.booklistRefresh(bookListView: self)
    }

    /// 排序
    func changeSort () {
        delegate?.bookChapterlistSort(bookListView: self)
    }
}

